﻿#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    HelpDialog = new Help(this);
    UI_init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::UI_init()
{
    //工具栏添加QLabel和弹簧，与action按钮拉开间隔
    QSpacerItem* spacer = new QSpacerItem(12, 12, QSizePolicy::Expanding, QSizePolicy::Expanding);  //弹簧
    QHBoxLayout* HLayout = new QHBoxLayout(this);
    HLayout->addItem(spacer);
    QLabel* winTitle = new QLabel("无边框桌面窗口应用模板V2.0",this);   //label作为窗口标题
    winTitle->setStyleSheet("color: rgb(255, 255, 255)");
    HLayout->addWidget(winTitle);

    QWidget* newWidget = new QWidget(this);
    newWidget->setLayout(HLayout);
    ui->toolBar->addWidget(newWidget);

     ui->statusbar->setStyleSheet("color: rgb(255, 255, 255)");
    //状态栏添加QLabel
//    QLabel* statusInfo = new QLabel(this);
//    statusInfo->setStyleSheet("color: rgb(255, 255, 255)");
//    ui->statusbar->addWidget(statusInfo);

    setWindowOpacity(1);    //窗口及控件全部透明
    bgColor.setRgb(85,85,127,255);   //窗口背景颜色

    setAttribute(Qt::WA_TranslucentBackground); //窗口透明，paintEvent实现颜色和透明度调整
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | windowFlags());    //窗口无边框&置顶

    quitMbox.setWindowFlags(Qt::WindowStaysOnTopHint);  //QMessageBox弹窗置顶。若主窗口未置顶，则弹窗无需置顶
    //Mbox添加按钮yes/no
    quitMbox.addButton(QMessageBox::No);
    quitMbox.addButton(QMessageBox::Yes);
    quitMbox.setText("确定退出吗？");
    quitMbox.setWindowTitle("退出确认");
}

//重写MainWindow类的event函数，可接收窗口的所有事件
bool MainWindow::event(QEvent *sys_event)
{
/*
    if(sys_event->type() == QEvent::KeyPress)//如果事件类型为键盘按下
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(sys_event);//将QEvent对象转换为QKeyEvent对象
        if(keyEvent->key() == Qt::Key_Escape)//如果按下的是ESC键
        {
            quitMbox.exec();
            if(quitMbox.result() == QMessageBox::Yes)
            {
                this->close();
            }
            return true;//已经处理的事件，返回true则不再进行事件分发
        }
    }
*/
    //按照原来的流程来进行事件的分发
    return QWidget::event(sys_event);
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(rect(), bgColor);
}

void MainWindow::mousePressEvent(QMouseEvent *mousePress)
{
    //mousePress->pos()为鼠标在当前窗口坐标系的坐标，在窗口坐标系中，窗口左上角坐标为(0,0)
    pressPoint = mousePress->pos();
    if(mousePress->button() == Qt::LeftButton)
    {
        pressMouseButton = Qt::LeftButton;
    }
    else
    {
        pressMouseButton = Qt::NoButton;
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *mouseMove)
{
    QRect toolBarRect = ui->toolBar->rect();  //工具栏矩形
    if(toolBarRect.contains(pressPoint))
    {
        //左键在工具栏按下时，可以拖动
        if(pressMouseButton == Qt::LeftButton)
        {
            /* 移动窗口时以左上角为基点。减去偏移量（鼠标在窗口坐标系中的坐标），使得鼠标移动时保持窗口和鼠标的相对位置不变 */
            move(mouseMove->globalPos() - pressPoint);
        }
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *mouseRelease)
{
    pressMouseButton = Qt::NoButton;
}

void MainWindow::keyPressEvent(QKeyEvent *keyPress)
{
    switch (keyPress->key())
    {
        case Qt::Key_F1:
        {
            float Opacity = windowOpacity() + 0.05;
            if(Opacity <= 1)
            {
                setWindowOpacity(Opacity);
                ui->statusbar->showMessage("透明度 " + QString::number(Opacity));
            }
            else
            {
                setWindowOpacity(1);
                ui->statusbar->showMessage("透明度 1");
            }
            break;
        }
        case Qt::Key_F2:
        {
            float Opacity = windowOpacity() - 0.05;
            if(Opacity >= 0)
            {
                setWindowOpacity(Opacity);
                ui->statusbar->showMessage("透明度 " + QString::number(Opacity));
            }
            else
            {
                setWindowOpacity(0);
                ui->statusbar->showMessage("透明度 0");
            }
            break;
        }
        case Qt::Key_F3:
        {
            QColorDialog selectColor;
            selectColor.setWindowFlags(Qt::WindowStaysOnTopHint);   //窗口置顶
            selectColor.setOption(QColorDialog::ShowAlphaChannel);  //允许设置Alpha透明度
            selectColor.exec();
            if(selectColor.result() == QColorDialog::Accepted)
            {
                bgColor = selectColor.currentColor();
            }
            else
            {
                qDebug()<<"error color.";
            }
            break;  //QColorDialog关闭后自动触发paintEvent，此处无需主动update()
        }
        case Qt::Key_F4:{setWindowState(Qt::WindowMinimized); break;}
        case Qt::Key_F5:{ui->toolBar->show(); break;}
        case Qt::Key_Escape:
        {
            quitMbox.exec();
            if(quitMbox.result() == QMessageBox::Yes)
            {
                this->close();
            }
            break;
        }
        //添加更多的key以完成其他功能
        default: break;
    }
}

void MainWindow::mouseDoubleClickEvent(QMouseEvent *mouseDoubleClick)
{

    QPoint ClickPoint = mouseDoubleClick->pos();
    QRect toolBarRect = ui->toolBar->rect();  //工具栏矩形
    if(toolBarRect.contains(ClickPoint))
    {
        if(windowState() == Qt::WindowMaximized)
        {
            setWindowState(Qt::WindowNoState);
        }
        else
        {
            setWindowState(Qt::WindowMaximized);
        }
    }
}

void MainWindow::on_actHelp_triggered()
{
    HelpDialog->show();
}

void MainWindow::on_actQuit_triggered()
{
    quitMbox.exec();
    if(quitMbox.result() == QMessageBox::Yes)
    {
        this->close();
    }
}

void MainWindow::on_acMinSize_triggered()
{
    setWindowState(Qt::WindowMinimized);
}

void MainWindow::on_actMaxSize_triggered()
{
    setWindowState(Qt::WindowMaximized);
}

void MainWindow::on_acRestore_triggered()
{
    setWindowState(Qt::WindowNoState);
}
